﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Domain.Campaigns;
using iOak.Common.Untity;
using iOak.Business.Campaigns;
using System.Transactions;
using iOak.Common;
using iOak.Business.Resumes;
using iOak.Domain.Resumes;
using System.Web.UI.HtmlControls;
namespace iOak.Web
{
    public partial class TalentDeclareList : BasePage
    {
        private static readonly CampaignService itemCampaignService = new CampaignService();
        private static readonly ApplicationService itemApplicationService = new ApplicationService();
        private static readonly ApplicationCandidateService itemApplicationCandidateService = new ApplicationCandidateService();
        private WorkAssistant _workassistant = new WorkAssistant();
        private List<Campaign> DataSource
        {
            get { return this.Session["TalentDeclareList_DataSource"] as List<Campaign>; }
            set { this.Session["TalentDeclareList_DataSource"] = value; }
        }

        private Company CompanyItem
        {
            get { return this.Session["TalentDeclareList_Company"] as Company; }
            set { this.Session["TalentDeclareList_Company"] = value; }
        }

        #region 画面初始化
        /// <summary>
        /// 画面初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            pagerControl.PageIndexChange += new Modules.PagerControl.OnPageIndexChangeHandler(pagerControl_PageIndexChange);
            if (!IsPostBack)
            {
                CompanyService companyService = new CompanyService();
                CompanyItem = companyService.Retrieve(_workassistant.CurrentUser.CompanyId);
                // 检索所有
                lnkAll_Click(sender, e);
            }
        }
        #endregion

        #region 翻页事件
        protected void pagerControl_PageIndexChange(object serder, EventArgs e)
        {
            pagerControl.BindData(gvTalentDeclareList, DataSource);
        }
        #endregion

        #region 行绑定
        /// <summary>
        /// gvCampaign行绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvTalentDeclareList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Campaign campaign = e.Row.DataItem as Campaign;
                var itemCurrent = itemApplicationService.GetListByCampaignIdAndCompanyId(campaign.CampaignId, _workassistant.CurrentUser.CompanyId);
                Guid? appId = null;
                int applicationStatus = 0;
                if (itemCurrent != null)
                {
                    appId = itemCurrent.ApplicationId;
                    applicationStatus = itemCurrent.Status;
                }
                Label lblResumeNumber = e.Row.FindControl("lblResumeNumber") as Label;
                if (appId != null)
                {
                    lblResumeNumber.Text = Convert.ToString(itemApplicationCandidateService.GetCount(applicationId: appId));
                }
                else
                {
                    lblResumeNumber.Text = "0";
                }
                Label lblStatus = e.Row.FindControl("lblStatus") as Label;
                lblStatus.Text = ResourceHelper.GetApplicationStatusName(applicationStatus);
                Panel pnlOptionMenu = e.Row.FindControl("pnlOptionMenu") as Panel;
                SetOptionMenu(itemCurrent, campaign, pnlOptionMenu);
                bool allowSubmit = true;
                //如果企业未被激活，则不能进行提交候选人
                if (!CompanyItem.IsActive.HasValue || !CompanyItem.IsActive.Value)
                {
                    allowSubmit = false;
                }
                PlaceHolder phSubmit = pnlOptionMenu.FindControl("phSubmit") as PlaceHolder;
                HtmlContainerControl divSubmit = phSubmit.FindControl("divSubmit") as HtmlContainerControl;
                String script = String.Format("$('.optionMenu').hide();submitClick('{0}',{1})", campaign.CampaignId.ToString(),
                                                                                               allowSubmit.ToString().ToLower());
                divSubmit.Attributes.Add("onclick", script);
            }
        }
        #endregion

        #region 回调按钮事件
        /// <summary>
        /// 回调按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPostBack_Click(object sender, EventArgs e)
        {
            //提交申请书
            ApplicationBase itemCurrent = itemApplicationService.GetListByCampaignIdAndCompanyId(new Guid(hidCampaignId.Value),
                                                                                                 _workassistant.CurrentUser.CompanyId);
            if (itemCurrent != null)
            {
                itemCurrent.Status = (int)ApplicationStatus.Submitted;
                itemApplicationService.Update(itemCurrent);
                // 检索所有
                lnkAll_Click(sender, e);
            }
        }
        #endregion

        #region 左边按钮点击事件
        protected void lnkAll_Click(object sender, EventArgs e)
        {
            ApplicationStatus status = ApplicationStatus.All;
            BindSearchData(status);
        }

        protected void lnkNoDeclaration_Click(object sender, EventArgs e)
        {
            ApplicationStatus status = ApplicationStatus.Open;
            BindSearchData(status);
        }

        protected void lnkYesDeclaration_Click(object sender, EventArgs e)
        {
            ApplicationStatus status = ApplicationStatus.Submitted;
            BindSearchData(status);
        }

        protected void lnkAudit_Click(object sender, EventArgs e)
        {
            ApplicationStatus status = ApplicationStatus.Checked;
            BindSearchData(status);
        }

        //protected void lnkChecking_Click(object sender, EventArgs e)
        //{
        //    ApplicationStatus status = ApplicationStatus.Checking;
        //    BindSearchData(status);
        //}
        #endregion

        #region 根据状态检索数据
        /// <summary>
        /// 根据状态检索数据
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        private void BindSearchData(ApplicationStatus status)
        {
            var returnList = new List<Campaign>();
            if (status == ApplicationStatus.All)
            {
                // 需要修改现在是根据当前公司的campaign和状态为1的campaign
                returnList = itemCampaignService.GetListCanBeUsedByCompany(_workassistant.CurrentUser.CompanyId);
                var statusList = itemCampaignService.GetListByStatus();
                foreach (var item in statusList)
                {
                    int count = returnList.Count(m => m.CampaignId.Equals(item.CampaignId));
                    if (count == 0)
                    {
                        returnList.Add(item);
                    }
                }

            }
            else
            {
                if (status == ApplicationStatus.Open)
                {
                    returnList = itemCampaignService.GetListCanSubmit(_workassistant.CurrentUser.CompanyId);
                }
                else
                {
                    returnList = itemCampaignService.GetListByApplicationStatus(_workassistant.CurrentUser.CompanyId, (int)status);
                }
            }
            DataSource = returnList;
            pagerControl.BindData(gvTalentDeclareList, DataSource);
        }
        #endregion

        #region 设定操作菜单
        /// <summary>
        /// 设定操作菜单
        /// </summary>
        /// <param name="status">Campaign状态</param>
        /// <param name="phOpenMenu">菜单容器</param>
        private void SetOptionMenu(ApplicationBase application, Campaign campaign, Panel pnlOptionMenu)
        {
            PlaceHolder phPreview = pnlOptionMenu.FindControl("phPreview") as PlaceHolder;
            PlaceHolder phAdd = pnlOptionMenu.FindControl("phAdd") as PlaceHolder;
            PlaceHolder phSubmit = pnlOptionMenu.FindControl("phSubmit") as PlaceHolder;
            PlaceHolder phDownLoadSummery = pnlOptionMenu.FindControl("phDownLoadSummery") as PlaceHolder;
            PlaceHolder phDownLoadApply = pnlOptionMenu.FindControl("phDownLoadApply") as PlaceHolder;
            bool overDue = false;
            //如果当前时间已超过活动截止日期，则不允许提交
            if (campaign.EndDate.CompareTo(DateTime.Now.Date) < 0)
            {
                overDue = true;
            }
            if (application == null)
            {
                phSubmit.Visible = false;
                phDownLoadSummery.Visible = false;
                phDownLoadApply.Visible = false;
                phAdd.Visible = !overDue;
            }
            else
            {
                switch (application.Status)
                {
                    case (int)CampaignStatus.Open:
                        phDownLoadSummery.Visible = false;
                        phDownLoadApply.Visible = false;
                        phSubmit.Visible = !overDue;
                        phAdd.Visible = !overDue;
                        break;
                    case (int)CampaignStatus.Appling:
                        phDownLoadSummery.Visible = false;
                        phDownLoadApply.Visible = false;
                        phSubmit.Visible = false;
                        break;
                    //case (int)CampaignStatus.Checking:
                    //    phAdd.Visible = false;
                    //    phSubmit.Visible = false;
                    //    phDownLoadSummery.Visible = false;
                    //    phDownLoadApply.Visible = false;
                    //    break;
                    case (int)CampaignStatus.Closed:
                        phAdd.Visible = false;
                        phSubmit.Visible = false;
                        break;
                }
            }
        }
        #endregion
    }
}
